import 'package:flutter/material.dart';
import 'package:x3_base_package/x3_base_package.dart';
import 'package:x3_business_core/business_customer/business_customer_details.dart';
import 'package:x3_business_core/business_customer/page/business_customer_details_coupons.dart';


class BusinessCustomerItem extends StatefulWidget {
  final Map<String, dynamic> item;
  final int index;
  final String queryType;
  final Function(Map<String,dynamic> item) onTapEvent;

  const BusinessCustomerItem({Key key, @required this.item, @required this.index, @required this.queryType, this.onTapEvent}) : super(key: key);

  @override
  _BusinessCustomerItemState createState() => _BusinessCustomerItemState();
}

class _BusinessCustomerItemState extends State<BusinessCustomerItem> {
  @override
  Widget build(BuildContext context) {
    return BaseCardComponent(
      onTap: jumpDepotVipDetailScreen,
      child: Container(
        width: double.infinity,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            buildMainInfo(),
            SizedBox(height: 8),
            BaseComponentUtils.buildHorizontalDivider(),
            SizedBox(height: 8),
            buildLastBuyDate(),
          ],
        ),
      ),
    );
  }

  /// 主要信息
  Widget buildMainInfo() {
    return Row(
      children: [
        buildLeftWidget(),
        SizedBox(width: 12.0),
        Expanded(child: buildRightWidget()),
      ],
    );
  }

  /// 左
  Widget buildLeftWidget() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        buildAvatar(),
        SizedBox(height: 4),
        buildBirthday(),
      ],
    );
  }

  /// 右
  Widget buildRightWidget() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            buildVipName(),
            SizedBox(width: 4),
            buildGradeName(),
            SizedBox(width: 4),
            buildPublicAccountSituation(),
            SizedBox(width: 4),
            buildEnterpriseWxSituation(),
          ],
        ),
        SizedBox(height: 4),
        buildPastYearConsumeFsums(),
        SizedBox(height: 4),
        buildVipScore(),
        SizedBox(height: 4),
        buildVipCouponNum(),
      ],
    );
  }

  /// 头像
  Widget buildAvatar() {
    String vipAvatarUrl = BaseUtils.getKeyData(widget.item, 'vipAvatarUrl');
    String vipName = BaseUtils.getKeyData(widget.item, 'vipName');
    return BaseImageComponent(imageUrl: vipAvatarUrl, placeholderName: vipName, size: BaseImageSizeEnum.original, type: BaseImageTypeEnum.X3User, width: 56.0, height: 56.0, circular: 56.0);
  }

  /// 生日
  Widget buildBirthday() {
    String vipBirthdayMonth = BaseUtils.getKeyData(widget.item, 'vipBirthdayMonth');
    String vipBirthdayDay = BaseUtils.getKeyData(widget.item, 'vipBirthdayDay');
    return Container(
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(horizontal: 6.0),
      constraints: BoxConstraints(minWidth: 56),
      decoration: BoxDecoration(border: Border.all(color: Color(0xFFDFDFDF), width: 0.8), borderRadius: BorderRadius.all(Radius.circular(12))),
      child: Container(alignment: Alignment.center, padding: EdgeInsets.only(bottom: 1.6), child: Text('$vipBirthdayMonth-$vipBirthdayDay')),
    );
  }

  /// 姓名
  Widget buildVipName() {
    String vipName = BaseUtils.getKeyData(widget.item, 'vipName');
    return Text(vipName, style: BaseStyles.bodyMedium(context).copyWith(fontSize: 15));
  }

  /// 卡等级
  Widget buildGradeName() {
    String gradeName = BaseUtils.getKeyData(widget.item, 'gradeName');
    return Text(gradeName, style: BaseStyles.subtitle2(context).copyWith(color: Color(0xFFBF9C4F)));
  }

  /// 公众号情况
  Widget buildPublicAccountSituation() {
    String wxNoPublic = BaseUtils.getKeyData(widget.item, 'wxNoPublic').toString();
    return Icon(BaseIconData.x3PublicAccount, size: 16, color: wxNoPublic == 'true' ? Color(0xFF34A853) : null);
  }

  /// 企业微信情况
  Widget buildEnterpriseWxSituation() {
    String enterpriseWx = BaseUtils.getKeyData(widget.item, 'enterpriseWx').toString();
    return Icon(BaseIconData.x3EnterpriseWX, size: 16, color: enterpriseWx == 'true' ? Color(0xFF34A853) : null);
  }

  /// 近一年实销
  Widget buildPastYearConsumeFsums() {
    String pastYearConsumeFsums = BaseUtils.getKeyData(widget.item, 'pastYearConsumeFsums', defaultValue: '0.0').toString();
    return BaseComponentUtils.buildTextSpanWidget(['近一年实销：', '¥$pastYearConsumeFsums'], styles: {0: BaseStyles.subtitle2(context), 1: BaseStyles.subtitle2(context)});
  }

  /// 积分
  Widget buildVipScore() {
    String vipScore = BaseUtils.getKeyData(widget.item, 'vipScore').toString();
    return BaseComponentUtils.buildTextSpanWidget(['积分：', vipScore], styles: {0: BaseStyles.subtitle2(context), 1: BaseStyles.subtitle2(context)});
  }

  /// 优惠券
  Widget buildVipCouponNum() {
    String vipCouponNum = BaseUtils.getKeyData(widget.item, 'vipCouponNum', defaultValue: '0').toString();
    return InkWell(
      onTap: jumpVipCoupons,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text('优惠券：', style: BaseStyles.subtitle2(context)),
          Text(vipCouponNum, style: TextStyle(decoration: TextDecoration.underline, color: Color(0xFF5E96F4))),
        ],
      ),
    );
  }

  /// 最近消费
  Widget buildLastBuyDate() {
    String lastBuyDate = BaseTransformUtil.stringToDateTimeStamp(BaseUtils.getKeyData(widget.item, 'lastBuyDate').toString(), isNeedShowSecond: false);
    return Row(
      children: [
        Text('最近消费：', style: BaseStyles.subtitle2(context)),
        Expanded(child: SizedBox()),
        Text(lastBuyDate, style: BaseStyles.subtitle2(context)),
      ],
    );
  }

  /// 跳转到优惠券页面
  void jumpVipCoupons() {
    String vipId = BaseUtils.getKeyData(widget.item, 'vipId');
    Map<String, dynamic> params = {'vipId': vipId};
    BaseRouterUtil.getInstance().pushNamed(BusinessCustomerDetailsCoupons.sName, arguments: params);
  }

  /// 跳转到会员详情
  void jumpDepotVipDetailScreen() {
    if (widget.onTapEvent != null) {
      widget.onTapEvent(widget.item);
      return;
    }
    String vipId = BaseUtils.getKeyData(widget.item, 'vipId');
    Map<String, dynamic> params = {'vipId': vipId, 'queryType': widget.queryType};
    BaseRouterUtil.getInstance().pushNamed(BusinessCustomerDetails.sName, arguments: params);
  }
}
